﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MixScript.DBService;
using MixScript.Appl.Security.Crypto;

namespace MixScript.BLL.ApplAdmin
{
    public class cCompro : cApplAdmin
    {
        private string sSQL = "";
        private byte[] LogoCompany;

        public byte[] LOGOCompany
        {
            get { return LogoCompany; }
            set { LogoCompany = value; }
        }
        public cCompro()
        {

        }
        public cCompro(string tablename)
        {
            Tablename = tablename;
        }

        public bool getCompro()
        {
            try
            {
                DataTable dt = new DataTable();
                string ss = "";
                return getCompro(ref dt, out ss);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool getCompro(ref DataTable dt, out string msg1)
        {
            try
            {
                msg1 = "";
                cAES_Cryptography oCrypt = new cAES_Cryptography();
                dt = new DataTable();                
                DataTable dtt = new DataTable();
                sSQL = "select * from tb_compro order by compid desc limit 1";
                dt = Dbserver.ExecuteQuery(sSQL, "tb_compro");
                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    int i = 0;
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (i > 0)
                        {
                            if (dc.DataType.Name == "String")
                            {
                                if (dc.ColumnName != "imgid")
                                {
                                    string s = oCrypt.decryptString(dr[dc.ColumnName].ToString(), cAES_Cryptography.encKeys);
                                    dr[dc.ColumnName] = s;// oCrypt.decryptString(dr[dc.ColumnName].ToString(), cSecurity.encKeys);
                                }
                            }
                        }
                        i++;
                    }
                    if (dr["imgid"].ToString() != string.Empty)
                    {
                        sSQL = "select imgdata from tb_imgpool where imgid='" + dr["imgid"].ToString() + "'";
                        dtt = Dbserver.ExecuteQuery(sSQL, "imgpool");
                        LogoCompany = (byte[])dtt.Rows[0]["imgdata"];
                    }
                    else
                    {
                        LogoCompany = null;
                    }

                    return true;

                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public override bool EditRowDtDB(out string msg1, string tablename, string[] keyfieldname, object[] keyvalue, bool haveHistory, params object[] fldValue)
        {
            try
            {
                msg1 = "";
                 cAES_Cryptography oCrypt = new cAES_Cryptography();
                DataTable dt = new DataTable();
                // inqTableName = "tb_transstudio";
                sSQL = "select * from " + tablename + " limit 1 order by compid desc";
                dt = Dbserver.ExecuteQuery(sSQL, tablename);
                if (dt.Rows.Count > 0)
                {
                    //  inqTableName = "vw_booking";
                    DataRow dr = dt.Rows[0];
                    int i = 0;
                    int j = 0;
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (i > 0 && fldValue[i].GetType().ToString() == "System.String")
                        {
                            dr[dc.ColumnName] = oCrypt.encryptString(fldValue[i].ToString(), cAES_Cryptography.encKeys);
                        }
                        i++;
                    }
                    if (Dbserver.openConn())
                    {

                        j = Dbserver.SqlUpdate(dt, tablename, out msg1);
                        if (j == 1)
                        {
                            msg1 = "Records Update Success. ";
                            Dbserver.CommitAndCloseConn();
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    msg1 = "Update Success";
                    return true;
                }
                else
                {
                    //    inqTableName = "vw_booking";
                    msg1 = "Records not Found";
                    return false;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public override bool AddNewRowDB(out string msg1, string tablename, int[] indexgenfieldname, string[] genid, bool haveHistory, params object[] fldValue)
        {
            try
            {

                cAES_Cryptography oCrypt = new cAES_Cryptography();
                msg1 = "";
                sSQL = "select * from " + tablename + " limit 0";
                DataTable dt = new DataTable();
                dt = Dbserver.ExecuteQuery(sSQL, tablename);

                DataRow dr = dt.NewRow();
                int i = 0;
                int j;
                foreach (int k in indexgenfieldname)
                {
                    fldValue[k] = new cDBHelper().genIDCommit(genid[k]);
                }
                //                fldValue[0] = new cDBHelper().genIDCommit("GENKDBRG");

                int x = 0;
                foreach (object ob in fldValue)
                {

                    if (ob.GetType().ToString() == "System.String" && x > 0)
                    {
                        //ob = "sdfsd";
                        // if(fldValue[x].ToString().Trim()!=string.Empty)
                        if (x != 19)
                            fldValue[x] = oCrypt.encryptString(ob.ToString(), cAES_Cryptography.encKeys);
                    }
                    x++;
                }
                i = 0;
                foreach (DataColumn dc in dt.Columns)
                {
                    if (i != 19)
                        dr[dc.ColumnName] = fldValue[i];
                    i++;
                }
                dt.Rows.Add(dr);
                if (Dbserver.openConn())
                {
                    string sid = "";
                    if (LogoCompany != null)
                    {
                        saveimagetodb(LogoCompany, out sid);
                        dt.Rows[0]["imgid"] = sid;
                    }
                    j = Dbserver.SqlUpdate(dt, tablename, out msg1);
                    if (j == 1)
                    {

                        msg1 = "Sukses";
                        Msg = msg1;

                        Dbserver.CommitAndCloseConn();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    msg1 = "Database unreachable";
                    Msg = msg1;
                    return false;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private void saveimagetodb(byte[] img, out string sid)
        {
            try
            {
                sid = "";


                if (Dbserver.openConn())
                {
                    saveimage(img, out sid);
                    // Dbserver.CommitAndCloseConn();
                }


            }
            catch (Exception ex)
            {

                throw;
            }
        }


    }
}
